package com.buaa.multitythread;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.buaa.adapter.MyAdapter;
import com.buaa.util.NetUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private ListView listView1;
    private MyAdapter adapter;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView1=(ListView) findViewById(R.id.listView);

        dialog=new ProgressDialog(this);
        dialog.setTitle("提示信息");
        dialog.setMessage("正在下载，请稍候...");
        dialog.setCancelable(false);//设置出现提示框时其他操作不可进行

        adapter=new MyAdapter(this);

        //调用异步任务
        new MyTask().execute(NetUtil.URL);

    }
    //开启一个异步任务
    public class MyTask extends AsyncTask<String,Void,List<Map<String,Object>>>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.show();
        }

        @Override
        protected void onPostExecute(List<Map<String, Object>> list) {
            super.onPostExecute(list);
            adapter.setData(list);
            listView1.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            dialog.dismiss();
        }

        @Override
        protected List<Map<String, Object>> doInBackground(String... params) {
            List<Map<String,Object>> list=new ArrayList<Map<String, Object>>();
            //获取网络数据
            try {
                HttpClient httpClient=new DefaultHttpClient();
                HttpPost httpPost=new HttpPost(params[0]);
                HttpResponse httpResponse=httpClient.execute(httpPost);
                if (httpResponse.getStatusLine().getStatusCode()==200){
                    String jsonString= EntityUtils.toString(httpResponse.getEntity(),"utf-8");
                    JSONObject jsonObject=new JSONObject(jsonString);
                    JSONArray jsonArray=jsonObject.getJSONArray("products");
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject1=jsonArray.getJSONObject(i);
                        Map<String,Object> map=new HashMap<String,Object>();
                        Iterator<String> iterator=jsonObject1.keys();
                        while (iterator.hasNext()){
                            String key=iterator.next();
                            Object value=jsonObject1.get(key);
                            map.put(key,value);
                        }
                        list.add(map);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return list;
        }
    }

}
